package com.ptszyxx.popose.module.base.login.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.utils.YLoginUtil;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.http.data.entity.login.LoginResult;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;
import com.ysg.utils.constant.BaseConstants;

import java.util.HashMap;
import java.util.Map;

public class LoginBindVM extends BaseViewModel<CommonRepository> {
    public ObservableField<String> phone = new ObservableField<>();
    public ObservableField<String> code = new ObservableField<>();
    public String thirdType, thirdToken;

    public LoginBindVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onImageCodeEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onSendCodeEvent = new SingleLiveEvent<>();
    }


    public BindingCommand onSendCodeCommand = new BindingCommand(() -> sendCode());
    public BindingCommand onSubmitCommand = new BindingCommand(() -> requestLoginBind());

    private void sendCode() {
        if (YStringUtil.isEmpty(phone.get())){
            YToastUtil.showShort(R.string.et_phone_hint);
            return;
        }
        requestCode();
        uc.onImageCodeEvent.call();
    }

    public void requestCode(){
        Map<String, String> map = new HashMap<>();
//        map.put("areaCode", );
        map.put("loginname", phone.get());
        map.put("packageName", BaseConstants.PACKAGE_NAME);

        HttpUtils.getInstance()
                .data(model.sendCode(map), this,
                        new OnSuccessResult() {
                            @Override
                            public void onSuccessResult(BaseResponse response) {
                                YToastUtil.showShort(R.string.send_success);
                                uc.onSendCodeEvent.call();
                            }
                        });
    }


    private void requestLoginBind(){
        if (!check())return;

        Map<String, String> map = new HashMap<>();
        map.put("loginname", phone.get());
        map.put("mobileCode", code.get());
        map.put("thirdType", thirdType);
        map.put("thirdToken", thirdToken);

        HttpUtils.getInstance()
                .data(model.loginBind(map), this,
                        new OnSuccessResult<LoginResult>() {
                            @Override
                            public void onSuccessResult(BaseResponse<LoginResult> response) {
                                YLoginUtil.getInstance().login(LoginBindVM.this, response.getData());
                            }
                        });
    }


    private boolean check(){
        if (YStringUtil.isEmpty(phone.get())){
            YToastUtil.showShort(R.string.et_phone_hint);
            return false;
        }
        if (YStringUtil.isEmpty(code.get())){
            YToastUtil.showShort(R.string.et_code_hint);
            return false;
        }
        return true;
    }
}
